package com.xbongbong.process.operate;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.xbongbong.paas.constant.MessageConstant;
import com.xbongbong.paas.constant.PaasConstant;
import com.xbongbong.paas.domain.entity.PaasCoUserEntity;
import com.xbongbong.paas.domain.entity.PaasFormDataEntity;
import com.xbongbong.paas.domain.entity.ext.OptionalRangeEntity;
import com.xbongbong.paas.domain.entity.ext.PaasFormEntityExt;
import com.xbongbong.paas.domain.entity.ext.PaasProcessTemplateEntityExt;
import com.xbongbong.paas.domain.entity.ext.PaasProcessTemplateNodeEntityExt;
import com.xbongbong.paas.enums.DelEnum;
import com.xbongbong.paas.enums.FieldTypeEnum;
import com.xbongbong.paas.enums.FlowStatusEnum;
import com.xbongbong.paas.enums.NodeTypeEnum;
import com.xbongbong.paas.enums.OptionalRangeEnum;
import com.xbongbong.paas.enums.SheetTypeEnum;
import com.xbongbong.paas.help.FormDataFormExplainHelper;
import com.xbongbong.paas.model.PaasCoUserModel;
import com.xbongbong.paas.model.PaasFormModel;
import com.xbongbong.paas.model.PaasProcessDataModel;
import com.xbongbong.paas.model.PaasProcessTemplateModel;
import com.xbongbong.paas.model.PaasProcessTemplateNodeModel;
import com.xbongbong.paas.pojo.dto.FormDataAddDTO;
import com.xbongbong.paas.pojo.dto.FormDataUpdateDTO;
import com.xbongbong.paas.pojo.dto.StartProcessDTO;
import com.xbongbong.paas.pojo.vo.StartProcessVO;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.util.BeanUtil;
import com.xbongbong.paas.toolbox.util.CollectionsUtil;
import com.xbongbong.paas.toolbox.util.CompareUtil;
import com.xbongbong.paas.toolbox.wrap.DepartmentVO;
import com.xbongbong.paas.toolbox.wrap.RoleSimpleVO;
import com.xbongbong.paas.toolbox.wrap.UserVO;
import com.xbongbong.pro.enums.SaasMarkEnum;
import com.xbongbong.pro.enums.errorcodes.SystemErrorCodeEnum;
import com.xbongbong.pro.formdata.pojo.SaasNeedRedundantAttrPojo;
import com.xbongbong.pro.formdata.pojo.vo.BeforeSaveVO;
import com.xbongbong.pro.invoice.pojo.InvoiceSpecialParamPojo;
import com.xbongbong.pro.service.cool.app.CoolAppProcessCardService;
import com.xbongbong.saas.constant.BasicConstant;
import com.xbongbong.saas.constant.ParameterConstant;
import com.xbongbong.saas.constant.StockConstant;
import com.xbongbong.saas.constant.StringConstant;
import com.xbongbong.saas.enums.FormTypeEnum;
import com.xbongbong.saas.enums.RedundantTemplateTypeEnum;
import com.xbongbong.saas.enums.UserTeamEnum;
import com.xbongbong.saas.enums.XbbRefTypeEnum;
import com.xbongbong.saas.enums.business.InstockEnum;
import com.xbongbong.saas.enums.business.InvoiceEnum;
import com.xbongbong.saas.toolbox.help.FastJsonHelper;
import com.xbongbong.sys.model.UserModel;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * Description: 开启流程help
 * @author 李杰
 * @date 2019/4/19 15:00
 * @version v1.0
 * @since v1.0
 */
@Component
public class ProcessStartHelp {

    private static  final Logger LOG = LoggerFactory.getLogger(ProcessStartHelp.class);

    @Resource
    private PaasProcessOperateService paasProcessOperateService;
    @Resource
    private PaasProcessDataModel paasProcessDataModel;
    @Resource
    private PaasProcessTemplateModel paasProcessTemplateModel;
    @Resource
    private PaasProcessTemplateNodeModel paasProcessTemplateNodeModel;
    @Resource
    private PaasCoUserModel paasCoUserModel;
    @Resource
    private FormDataFormExplainHelper formDataFormExplainHelper;
    @Resource
    private UserModel userModel;
    @Resource
    private PaasFormModel paasFormModel;
    @Resource
    private CoolAppProcessCardService coolAppProcessCardService;

    /**
     * 编辑，开始流程
     * @param formDataUpdateDTO
     * @param paasFormDataEntity
     * @param oldPaasFormDataEntity
     * @param paasFormEntityExt
     * @return
     * @throws XbbException
     */
    public Integer startProcess(FormDataUpdateDTO formDataUpdateDTO, PaasFormDataEntity paasFormDataEntity, PaasFormDataEntity oldPaasFormDataEntity,
                                   PaasFormEntityExt paasFormEntityExt, SaasNeedRedundantAttrPojo saasNeedRedundantAttrPojo) throws XbbException {
        Integer noApprovalRequired = 1;
        try {
            Integer businessType = formDataUpdateDTO.getBusinessType();
            JSONObject newData = formDataUpdateDTO.getDataList();
            if ( paasFormEntityExt.getIsProcessForm().equals(1) && !Objects.equals(formDataUpdateDTO.getIsImport(), 1) ) {
                StartProcessDTO startProcessDTO = new StartProcessDTO();
                InvoiceSpecialParamPojo invoiceSpecialParamPojo = null;
                //销项发票关联合同操作进审批前的特殊处理
                if(Objects.nonNull(saasNeedRedundantAttrPojo)){
                    invoiceSpecialParamPojo = saasNeedRedundantAttrPojo.getInvoiceSpecialParamPojo();
                }
                boolean isInvoiceProcess =Objects.equals(formDataUpdateDTO.getBusinessType(),XbbRefTypeEnum.INVOICE.getCode())
                        && Objects.nonNull(invoiceSpecialParamPojo) && Objects.equals(invoiceSpecialParamPojo.getInvoiceLinkContractType(), BasicConstant.ONE);
                //适用场景：从销项发票->关联合同的入口，编辑进审批的场景，所以需要老数据覆盖新数据
                if(isInvoiceProcess){
                    JSONArray contractArray = FastJsonHelper.getJsonArrOrDefaultFromFormData(newData, InvoiceEnum.CONTRACT_ID.getAttr(), null);
                    Long invoiceDate = FastJsonHelper.getLongFromFormData(newData, InvoiceEnum.INVOICE_DATE.getAttr());
                    JSONObject originData = oldPaasFormDataEntity.getData();
                    newData.putAll(originData);
                    newData.put(InvoiceEnum.INVOICE_DATE.getAttr(), invoiceDate);
                    if (CollectionsUtil.isNotEmpty(contractArray)){
                        newData.put(InvoiceEnum.CONTRACT_ID.getAttr(), contractArray);
                        formDataUpdateDTO.setDataList(newData);
                    }
                }
                BeanUtil.copyProperties(formDataUpdateDTO, startProcessDTO);
                startProcessDTO.setOwnerId(paasFormDataEntity.getOwnerId());
                startProcessDTO.setData(formDataUpdateDTO.getDataList());
                startProcessDTO.setFormDataId(formDataUpdateDTO.getDataId());
                startProcessDTO.setCreatorId(formDataUpdateDTO.getUserId());
                startProcessDTO.setSaasNeedRedundantAttrPoJo(saasNeedRedundantAttrPojo);
                SheetTypeEnum sheetTypeEnum = SheetTypeEnum.getByBusinessType(businessType);
                boolean sheetFlag = SheetTypeEnum.validateSheet(sheetTypeEnum);
                if (sheetFlag) {
                    startProcessDTO.setBusinessType(sheetTypeEnum.getXbbRefTypeEnum().getCode());
                }
                StartProcessVO startProcessVO = startProcess(startProcessDTO, oldPaasFormDataEntity);
                if ( Objects.isNull(startProcessVO) ) {
                    throw new XbbException(SystemErrorCodeEnum.API_ERROR_100001, MessageConstant.START_PROCESS_ERROR);
                }
                noApprovalRequired = startProcessVO.getNoApprovalRequired();
            }
        } catch (XbbException e) {
            throw e;
        } catch (Exception e) {
            LOG.error("abstractFormDataUpdate.startProcess 出错：", e);
            throw new XbbException(SystemErrorCodeEnum.API_ERROR_100001);
        }
        return noApprovalRequired;
    }

    /**
     * 新建 进入流程
     * @param formDataAddDTO
     * @param beforeSaveVO
     * @return java.lang.Integer 1：无需审批，后面需要进行插入操作 非1：进入审批，后面不需要进行插入操作
     * @author 吴峰
     * @date 2021/2/3 15:05
     * @throws XbbException
     */
    public Integer startProcess(FormDataAddDTO formDataAddDTO, BeforeSaveVO beforeSaveVO) throws XbbException  {
        Integer noApprovalRequired = 1;
        try {
            Integer businessType = formDataAddDTO.getBusinessType();
            PaasFormDataEntity newPaasFormDataEntity = beforeSaveVO.getPaasFormDataEntity();
            PaasFormEntityExt paasFormEntityExt = beforeSaveVO.getPaasFormEntityExt();
            if (paasFormEntityExt.getIsProcessForm().equals(1) && !Objects.equals(formDataAddDTO.getIsImport(), 1)
                    && !Objects.equals(1, formDataAddDTO.getIsBusinessRule())) {
                StartProcessDTO startProcessDTO = new StartProcessDTO();
                BeanUtil.copyProperties(formDataAddDTO, startProcessDTO);
                SheetTypeEnum sheetTypeEnum = SheetTypeEnum.getByBusinessType(businessType);
                boolean sheetFlag = SheetTypeEnum.validateSheet(sheetTypeEnum);
                if (sheetFlag) {
                    startProcessDTO.setBusinessType(sheetTypeEnum.getXbbRefTypeEnum().getCode());
                }
                startProcessDTO.setOwnerId(newPaasFormDataEntity.getOwnerId());
                startProcessDTO.setCreatorId(newPaasFormDataEntity.getCreatorId());
                startProcessDTO.setData(formDataAddDTO.getDataList());
                startProcessDTO.setSaasNeedRedundantAttrPoJo(beforeSaveVO.getSaasNeedRedundantAttrPojo());
                // app首页常用表单进入的menuId不对
                if ( Objects.equals(businessType, XbbRefTypeEnum.CUSTOMER_MANAGEMENT.getCode()) ) {
                    PaasFormEntityExt paasFormEntity = paasFormModel.getByBusinessType(XbbRefTypeEnum.CUSTOMER_MANAGEMENT.getCode(), formDataAddDTO.getCorpid());
                    startProcessDTO.setMenuId(paasFormEntity.getMenuId());
                } else if ( Objects.equals(businessType, XbbRefTypeEnum.SALES_OPPORTUNITY.getCode()) ) {
                    PaasFormEntityExt paasFormEntity = paasFormModel.getByBusinessType(XbbRefTypeEnum.SALES_OPPORTUNITY.getCode(), formDataAddDTO.getCorpid());
                    startProcessDTO.setMenuId(paasFormEntity.getMenuId());
                }
                StartProcessVO startProcessVO = startProcess(startProcessDTO, null);
                if ( Objects.isNull(startProcessVO) ) {
                    throw new XbbException(SystemErrorCodeEnum.API_ERROR_100001, MessageConstant.START_PROCESS_ERROR);
                }
                noApprovalRequired = startProcessVO.getNoApprovalRequired();
                // 酷应用
                coolAppProcessCardService.coolAppCard(startProcessDTO, startProcessVO);
            }
        } catch (XbbException e) {
            throw e;
        } catch (Exception e) {
            LOG.error("abstractFormDataService.startProcess出错", e);
            throw new XbbException(SystemErrorCodeEnum.API_ERROR_100001);
        }
        return noApprovalRequired;
    }

    public StartProcessVO startProcess(StartProcessDTO startProcessDTO, PaasFormDataEntity oldPaasFormDataEntity) throws XbbException {
        StartProcessVO startProcessVO = new StartProcessVO();
        try {
            // 查询审批模板
            Map<String, Object> params = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
            params.put("del", 0);
            Integer businessType = startProcessDTO.getBusinessType();
            // saas表单可能有多个新建入口，所以有可能formId不一致导致无法查询到数据
            if (Objects.equals(startProcessDTO.getSaasMark(), SaasMarkEnum.SAAS.getCode())) {
                //注：预收款余额、预付款余额列表内新建的回款单，需要对其businessType进行处理
                boolean flag = RedundantTemplateTypeEnum.isMultiTemplate(businessType) || Objects.equals(businessType, XbbRefTypeEnum.WORK_ORDER.getCode()) || FormTypeEnum.isMultiSingleTemplate(businessType);
                // 联系人和退货退款也是特殊多模板了
                if (flag || Arrays.asList(XbbRefTypeEnum.CONTACT.getCode(), XbbRefTypeEnum.REFUND.getCode()).contains(businessType)) {
                    params.put("formId", startProcessDTO.getFormId());
                }
                params.put("businessType", businessType);
            } else {
                params.put("formId", startProcessDTO.getFormId());
            }
            params.put("saasMark", startProcessDTO.getSaasMark());
            params.put("corpid", startProcessDTO.getCorpid());
            params.put("enable", 1);
            List<PaasProcessTemplateEntityExt> processList = paasProcessTemplateModel.list(params);
            if (processList.size() > 1) {
                throw new XbbException(SystemErrorCodeEnum.API_ERROR_100001, MessageConstant.PROCESS_OPEN_ERROR);
            }
            // 防止前端返回空数据进审批
            if ((Objects.isNull(startProcessDTO.getData()) || startProcessDTO.getData().isEmpty())) {
                startProcessVO.setNoApprovalRequired(0);
                return startProcessVO;
            }

            if (!processList.isEmpty()) {
                PaasProcessTemplateEntityExt paasProcessTemplateEntityExt = processList.get(0);
                if (startProcessDTO.getFormDataId() != null && !Objects.equals(startProcessDTO.getFormDataId(), 0L)) {
                    params.put("formId", startProcessDTO.getFormId());
                    params.put("businessType", businessType);
                    params.put("flowStatusIn", Arrays.asList(FlowStatusEnum.IN_APPROVAL.getType(), FlowStatusEnum.APPROVAL_PENDING.getType()));
                    params.put("formDataId", startProcessDTO.getFormDataId());
                    Integer count = paasProcessDataModel.getEntitysCount(params);
                    if (count > 0) {
                        throw new XbbException(SystemErrorCodeEnum.API_ERROR_100001, MessageConstant.DATA_HAVE_IN_PROCESS);
                    }
                }

                // 无需审批角色判断
                Set<String> roleList = new HashSet<>();
                Set<String> departmentList = new HashSet<>();
                Set<String> userList = new HashSet<>();
                List<OptionalRangeEntity> rolesWithoutApproveList = paasProcessTemplateEntityExt.getRolesWithoutApproveList();
                for(OptionalRangeEntity optionalRangeEntity: rolesWithoutApproveList) {
                    if(Objects.equals(optionalRangeEntity.getProperty(), OptionalRangeEnum.DEPT.getValue())){
                        departmentList.add(optionalRangeEntity.getId());
                    } else if (Objects.equals(optionalRangeEntity.getProperty(), OptionalRangeEnum.ROLE.getValue())){
                        roleList.add(optionalRangeEntity.getId());
                    } else if (optionalRangeEntity.getProperty().equals(OptionalRangeEnum.USER.getValue())){
                        userList.add(optionalRangeEntity.getId());
                    }
                }
                // fix 24764
                String creatorId = startProcessDTO.getCreatorId();
                UserVO userVO = userModel.getUserVO(startProcessDTO.getCorpid(), creatorId, true, false);
                Set<RoleSimpleVO> roleSimpleVOSet = userVO.getRoleSet();
                Set<DepartmentVO> departmentVOSet = userVO.getDepSet();
                if (!userList.isEmpty()) {
                    if (userList.contains(creatorId)) {
                        return startProcessVO;
                    }
                }
                if (!roleList.isEmpty()) {
                    for (RoleSimpleVO roleSimpleVO : roleSimpleVOSet) {
                        if (roleList.contains(roleSimpleVO.getId().toString())) {
                            return startProcessVO;
                        }
                    }
                }
                if (!departmentList.isEmpty()) {
                    for (DepartmentVO departmentVO : departmentVOSet) {
                        if (departmentList.contains(departmentVO.getId().toString())) {
                            return startProcessVO;
                        }
                    }
                }
                boolean isNeedApprove = false;
                if (Objects.nonNull(oldPaasFormDataEntity)) {
                    List<String> needApproveFieldList = paasProcessTemplateEntityExt.getNeedApproveFieldList();
                    JSONObject newData = (JSONObject) startProcessDTO.getData().clone();
                    newData.put("serialNo", startProcessDTO.getSerialNo());
                    newData.put(FieldTypeEnum.DEPARTMENTID.getAlias(), startProcessDTO.getDepartmentId());
                    JSONObject oldData = (JSONObject) oldPaasFormDataEntity.getData().clone();
                    oldData.put("serialNo", oldPaasFormDataEntity.getSerialNo());
                    oldData.put(FieldTypeEnum.DEPARTMENTID.getAlias(), oldPaasFormDataEntity.getDepartmentId());
                    if (UserTeamEnum.hasTeam(businessType)) {
                        Map<Long, List<String>> mainUserMap = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
                        Map<Long, List<String>> coUserMap = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
                        boolean isPublic = Objects.equals(XbbRefTypeEnum.CUSTOMER_PUBLIC.getCode(), startProcessDTO.getSubBusinessType()) || Objects.equals(XbbRefTypeEnum.CLUE_PUBLIC.getCode(), startProcessDTO.getSubBusinessType());
                        formDataFormExplainHelper.getUserIdMap(Collections.singletonList(oldPaasFormDataEntity.getId()),startProcessDTO.getCorpid(), businessType,isPublic,mainUserMap,coUserMap);
                        List<String> mainUserList = mainUserMap.get(oldPaasFormDataEntity.getId());
                        List<String> coUserList = coUserMap.get(oldPaasFormDataEntity.getId());
                        oldData.put(FieldTypeEnum.OWNERID.getAlias(), mainUserList);
                        oldData.put(FieldTypeEnum.COUSERID.getAlias(), coUserList);
                    } else {
                        oldData.put(FieldTypeEnum.OWNERID.getAlias(), Arrays.asList(oldPaasFormDataEntity.getOwnerId()));
                        // 协同人
                        Map<String, Object> map = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
                        map.put("appId", startProcessDTO.getAppId());
                        map.put("formId", startProcessDTO.getFormId());
                        map.put("formDataIdIn", Collections.singletonList(oldPaasFormDataEntity.getId()));
                        map.put("del", 0);
                        List<PaasCoUserEntity> list = paasCoUserModel.list(map);
                        if (!list.isEmpty()) {
                            List<String> coUserIdList = new ArrayList<>();
                            list.forEach(item-> coUserIdList.add(item.getCoUserId()));
                            oldData.put(FieldTypeEnum.COUSERID.getAlias(), coUserIdList);
                        }
                    }

                    isNeedApprove = isNeedApprove(newData, oldData, needApproveFieldList);
                }
                if(paasProcessTemplateEntityExt.getEnable().equals(1) && (oldPaasFormDataEntity == null || isNeedApprove)){
                    // 线索转换客户操作导致客户进了审批，带了这两个参数进来，因为前面data清掉了这两个不在解释当中的字段，所以这边用dto过渡一下
                    if(!paasProcessTemplateEntityExt.getBusinessType().equals(XbbRefTypeEnum.CONTACT.getCode())){
                        if (Objects.nonNull(startProcessDTO.getClueId())) {
                            startProcessDTO.getData().put("clueId", startProcessDTO.getClueId());
                        }
                        if (Objects.nonNull(startProcessDTO.getIsBringCustomer())) {
                            startProcessDTO.getData().put("isBringCustomer", startProcessDTO.getIsBringCustomer());
                        }
                    }
                    startProcessDTO.setData(startProcessDTO.getData());
                    startProcessDTO.setFormDataId(startProcessDTO.getFormDataId());
                    startProcessDTO.setFlowStatus(FlowStatusEnum.APPROVAL_PENDING.getType());
                    startProcessDTO.setTemplateId(paasProcessTemplateEntityExt.getId());
                    startProcessDTO.setMenuId(paasProcessTemplateEntityExt.getMenuId());
                    if (StockConstant.STOCK_TYPE_LIST.contains(businessType)) {
                        startProcessDTO.setStockType(startProcessDTO.getData().getInteger(InstockEnum.TYPE.getAttr()));
                    }
                    startProcessVO = paasProcessOperateService.dingStartProcess(startProcessDTO);
                    if(startProcessVO == null) {
                        throw new XbbException(SystemErrorCodeEnum.API_ERROR_100001, MessageConstant.START_PROCESS_ERROR);
                    }
                }
            }
        } catch (XbbException e) {
            throw e;
        } catch (Exception e) {
            LOG.error("formData.startProcess流程任务生成失败", e);
            throw new XbbException(SystemErrorCodeEnum.API_ERROR_100001, SystemErrorCodeEnum.API_ERROR_100001.getMsg());
        }
        return startProcessVO;
    }


    public static boolean isNeedApprove(JSONObject data, JSONObject oldData, List<String> needApproveFieldList) {
        return CompareUtil.isNeedApprove(data, oldData, needApproveFieldList);
    }

    /**
     * 获取流程模板
     * @return
     * @throws XbbException
     */
    public List<PaasProcessTemplateEntityExt>  getProcessTemplateList(String corpid, Long formId, Long appId) throws XbbException {
        List<PaasProcessTemplateEntityExt> processTemplateList;
        try {
            Map<String, Object> processTemplateParam = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
            processTemplateParam.put("del", DelEnum.NORMAL.getDel());
            processTemplateParam.put("formId", formId);
            processTemplateParam.put("appId", appId);
            processTemplateParam.put("enable", 1);
            processTemplateParam.put("usePermission", 1);
            processTemplateParam.put(StringConstant.CORPID, corpid);
            processTemplateParam.put(ParameterConstant.COLUMNS, " enable, id ");
            processTemplateList = paasProcessTemplateModel.list(processTemplateParam);
        }catch (Exception e) {
            LOG.error("processStartHelp.getProcessTemplateList 获取流程模板出错：", e);
            throw new XbbException(SystemErrorCodeEnum.API_ERROR_100001);
        }
        return processTemplateList;
    }

    public PaasProcessTemplateNodeEntityExt getStartNode(String corpid, Long formId, Long appId, PaasProcessTemplateEntityExt paasProcessTemplateEntityExt) throws XbbException{
        List<PaasProcessTemplateNodeEntityExt> nodeList;
        try {
            Map<String, Object> param = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
            param.put("type", NodeTypeEnum.START_PROCESS_NODE.getNodeType());
            param.put("appId", appId);
            param.put("formId", formId);
            param.put("templateId", paasProcessTemplateEntityExt.getId());
            param.put(StringConstant.CORPID, corpid);
            param.put(StringConstant.DEL, DelEnum.NORMAL.getDel());
            nodeList = paasProcessTemplateNodeModel.list(param);
            if(nodeList == null || nodeList.isEmpty()){
                throw new XbbException(SystemErrorCodeEnum.API_ERROR_100001, MessageConstant.PROCESS_NODE_NOT_EXIST);
            }
        }catch (Exception e) {
            LOG.error("processStartHelp.getStartNode 获取流程模板的起始节点出错：", e);
            throw new XbbException(SystemErrorCodeEnum.API_ERROR_100001);
        }
        return nodeList.get(0);
    }

    /**
     * 判断表单是否开启流程，只限于单模版情况，多模版不适用
     * @param corpid 公司ID
     * @param businessType 业务code
     * @return 是否为流程表单
     * @throws XbbException error
     * @author zhouwq
     * @date 2020/7/7 9:18
     */
    public boolean processFormJudge(String corpid, Integer businessType) throws XbbException{
        Map<String, Object> processTemplateParam = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
        processTemplateParam.put("del", DelEnum.NORMAL.getDel());
        processTemplateParam.put("businessType", businessType);
        processTemplateParam.put("enable", 1);
        processTemplateParam.put(StringConstant.CORPID, corpid);
        processTemplateParam.put(ParameterConstant.COLUMNS, " enable, id ");
        List<PaasProcessTemplateEntityExt> processTemplateList;
        try {
            processTemplateList = paasProcessTemplateModel.list(processTemplateParam);
        }catch (Exception e) {
            LOG.error("processStartHelp.getProcessTemplateList 获取流程模板出错：", e);
            throw new XbbException(SystemErrorCodeEnum.API_ERROR_100001);
        }
        return processTemplateList != null && processTemplateList.size() > 0;
    }


}
